import json
import time
import logging

from QUANTAXIS_RealtimeCollector.QARealtimeCollector.collectors.stock_min_bar_collector import QARTCStockMinBar
from QUANTAXIS_RealtimeCollector.QARealtimeCollector.collectors.stock_day_bar_collector import QARTCStockDayBar
from QUANTAXIS_RealtimeCollector.QARealtimeCollector.datahandler.stock_resampler import QARTCStockBarResampler

def main():
    # rtc
    delay = 30  # 更新间隔
    # 分钟线
    minbar = QARTCStockMinBar(delay=delay, debug=False, frequency='1min')
    minbar.start()
    # 日线
    daybar = QARTCStockDayBar(delay=delay, debug=False, frequency='1day')
    daybar.start()
    print('Start realtime collector')

    # resample
    freqs = ['5min', '15min']
    for f in freqs:
        QARTCStockBarResampler(frequency=f).start()
        print('Resample codes to %s' % f)
        time.sleep(1)


if __name__ == "__main__":
    try:
        from QUANTAXIS_RealtimeCollector.QARealtimeCollector.utils.logconf import update_log_file_config
        logfile = 'stock.sim.log'
        logging.config.dictConfig(update_log_file_config(logfile))
    except Exception as e:
        print(e.__str__())
    main()
